#define TILT 1

int chuck(int info){
  int value = 0;
  Wire.requestFrom (0x52, 6);	// request data from nunchuck
  while (Wire.available ())
    {
      outbuf[cnt] = nunchuk_decode_byte (Wire.read ());	// receive byte as an integer
      digitalWrite (ledPin, HIGH);	// sets the LED on
      cnt++;
    }

  // If we recieved the 6 bytes, then go print them
  if ((cnt >= 5)&&(info == TILT))
    {
      value = accel_y();
    }

  cnt = 0;
  send_zero (); // send the request for next bytes
  delay (100);
  return value;  
}

int accel_y ()
{
  int accel_y_axis = outbuf[3] * 2 * 2;

  if ((outbuf[5] >> 4) & 1)
    {
      accel_y_axis += 2;
    }
  if ((outbuf[5] >> 5) & 1)
    {
      accel_y_axis += 1;
    }

  
return accel_y_axis;
  
}



void send_zero ()
{
  Wire.beginTransmission (0x52);	// transmit to device 0x52
  Wire.write (0x00);		// sends one byte
  Wire.endTransmission ();	// stop transmitting
}



void nunchuck_init ()
{
  Wire.beginTransmission (0x52);	// transmit to device 0x52
  Wire.write (0x40);		// sends memory address
  Wire.write (0x00);		// sends sent a zero.  
  Wire.endTransmission ();	// stop transmitting
}


char nunchuk_decode_byte (char x)
{
  x = (x ^ 0x17) + 0x17;
  return x;
}
